package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.dto.LegalCasesQueryDTO;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.entity.LegalCases;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.cms.service.LegalCasesService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/legalCases")
public class LegalCasesController {
    @Autowired
    LegalCasesService legalCasesService;
    @Autowired
    private ColumnInfoService columnInfoService;

    @RequestMapping("/list")
    public String index(HttpServletRequest request, Model model) {
        String name = request.getParameter("name");
        String industryTypeId = request.getParameter("industryType");
        String mediaTypeId = request.getParameter("mediaType");
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");

        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }


        String hylxId = "";
        List<ColumnInfo> ggfgCoulumnInfo = columnInfoService.findByCode(Constants.CODE_FLFK_HYLX); // 法律法规
        if (!CollectionUtils.isEmpty(ggfgCoulumnInfo)) {
            ColumnInfo ggfg = ggfgCoulumnInfo.get(0);
            hylxId = ggfg.getId();
        }
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setRootColumnId(hylxId);
        columnInfoQueryDTO.setIsRootColumnLike(false);
        List<ColumnInfo> hylxIdCoulumnInfoList = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);

        String mtlxId = "";
        List<ColumnInfo> bgtCoulumnInfo = columnInfoService.findByCode(Constants.CODE_FLFK_MTLX);//曝光台
        if (!CollectionUtils.isEmpty(bgtCoulumnInfo)) {
            ColumnInfo bgt = bgtCoulumnInfo.get(0);
            mtlxId = bgt.getId();
        }
        ColumnInfoQueryDTO bgtIdColumnInfoQueryDTO = new ColumnInfoQueryDTO();
        bgtIdColumnInfoQueryDTO.setRootColumnId(mtlxId);
        bgtIdColumnInfoQueryDTO.setIsRootColumnLike(false);
        List<ColumnInfo> mtlxIdCoulumnInfoList = columnInfoService.queryColumnInfoList(bgtIdColumnInfoQueryDTO);


        LegalCasesQueryDTO legalCasesQueryDTO = new LegalCasesQueryDTO();
        legalCasesQueryDTO.setName(name);
        legalCasesQueryDTO.setCurrentPage(currentPage);
        legalCasesQueryDTO.setPageSize(pageSize);
        legalCasesQueryDTO.setIndustryType(industryTypeId);
        legalCasesQueryDTO.setMediaType(mediaTypeId);

        PageModel<LegalCases> page = legalCasesService.queryLegalCasesPage(legalCasesQueryDTO);
        List<LegalCases> list = page.getList();
        list.forEach(legalCases -> {
            String industryType = legalCases.getIndustryType();
            String mediaType = legalCases.getMediaType();
            ColumnInfo industryTypeColumnInfo=null;
            ColumnInfo mediaTypeColumnInfo =null;
            if(StringUtils.isNotBlank(industryType)){
                 industryTypeColumnInfo = columnInfoService.find(industryType);
            }
            if(StringUtils.isNotBlank(mediaType)){
                 mediaTypeColumnInfo = columnInfoService.find(mediaType);
            }
            if (industryTypeColumnInfo != null) {
                legalCases.setIndustryTypeName(industryTypeColumnInfo.getName());
            }
            if (mediaTypeColumnInfo != null) {
                legalCases.setMediaTypeName(mediaTypeColumnInfo.getName());
            }
        });

        model.addAttribute("page", page);
        model.addAttribute("legalCasesQueryDTO", legalCasesQueryDTO);
        model.addAttribute("hylxIdCoulumnInfoList", hylxIdCoulumnInfoList);
        model.addAttribute("mtlxIdCoulumnInfoList", mtlxIdCoulumnInfoList);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_LEGAL_CASES);
        return "/cms/LegalCases/legal_cases_list";
    }

    @RequestMapping("/edit")
    public String dialogEdit(HttpServletRequest request, Model model) {

        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            LegalCases legalCases = legalCasesService.find(id);
            model.addAttribute("legalCases", legalCases);
        }
        String hylxId = "";
        List<ColumnInfo> ggfgCoulumnInfo = columnInfoService.findByCode(Constants.CODE_FLFK_HYLX); // 行业类型
        if (!CollectionUtils.isEmpty(ggfgCoulumnInfo)) {
            ColumnInfo ggfg = ggfgCoulumnInfo.get(0);
            hylxId = ggfg.getId();
        }
        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setRootColumnId(hylxId);
        columnInfoQueryDTO.setIsRootColumnLike(false);
        List<ColumnInfo> hylxIdCoulumnInfoList = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);

        String mtlxId = "";
        List<ColumnInfo> bgtCoulumnInfo = columnInfoService.findByCode(Constants.CODE_FLFK_MTLX);//媒体类型
        if (!CollectionUtils.isEmpty(bgtCoulumnInfo)) {
            ColumnInfo bgt = bgtCoulumnInfo.get(0);
            mtlxId = bgt.getId();
        }
        ColumnInfoQueryDTO bgtIdColumnInfoQueryDTO = new ColumnInfoQueryDTO();
        bgtIdColumnInfoQueryDTO.setRootColumnId(mtlxId);
        bgtIdColumnInfoQueryDTO.setIsRootColumnLike(false);
        List<ColumnInfo> mtlxIdCoulumnInfoList = columnInfoService.queryColumnInfoList(bgtIdColumnInfoQueryDTO);

        model.addAttribute("hylxIdCoulumnInfoList", hylxIdCoulumnInfoList);
        model.addAttribute("mtlxIdCoulumnInfoList", mtlxIdCoulumnInfoList);

        return "/cms/LegalCases/dialog/legal_cases_edit";
    }


    @RequestMapping("/ajax/save")
    @ResponseBody
    public AjaxResult doFriendShipAjaxSave(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {

            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String industryTypeId = request.getParameter("industryType");
            String mediaTypeId = request.getParameter("mediaType");
            String coverImg = request.getParameter("coverImg");
            String description = request.getParameter("description");
            String analysis = request.getParameter("analysis");
            LegalCases legalCases = new LegalCases();
            legalCases.setName(name);
            legalCases.setId(id);
            legalCases.setDescription(description);
            legalCases.setIndustryType(industryTypeId);
            legalCases.setMediaType(mediaTypeId);
            legalCases.setCoverImg(coverImg);
            legalCases.setAnalysis(analysis);
            if (StringUtils.isNotBlank(id)) {
                legalCasesService.updateSelectiveById(legalCases);
            } else {
                legalCasesService.save(legalCases);
            }
            ajaxResult.setSuccess(true);

        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return ajaxResult;
    }

    /**
     * 栏目保存操作
     *
     * @param request
     * @return
     */
    @RequestMapping("/ajax/del")
    @ResponseBody
    public AjaxResult doFriendShipAjaxDel(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            legalCasesService.delete(id);
            ajaxResult.setSuccess(true);

        } catch (Exception e) {
            ajaxResult.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return ajaxResult;
    }

}
